package com.hitqz.robot.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.model.dto.PatrolObjectStandardDto;
import com.hitqz.robot.api.business.model.vo.patrol.PatrolObjectStandardVo;
import com.hitqz.robot.api.common.entity.AlarmRule;
import com.hitqz.robot.api.common.entity.PatrolObjectStandard;
import com.hitqz.robot.biz.service.AlarmRuleService;
import com.hitqz.robot.biz.service.PatrolObjectStandardService;
import com.hitqz.robot.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wengyx
 * @date 2024/8/7
 */

@RestController
@AllArgsConstructor
@RequestMapping("/patrolStandard")
@Tag(name = "巡检对象标准值管理")
public class PatrolObjectStandardController {

    private final PatrolObjectStandardService patrolObjectStandardService;
    private final AlarmRuleService alarmRuleService;


    @GetMapping("/{id}")
    @Operation(summary = "根据id查询巡检对象标准值")
    public R<PatrolObjectStandardVo> getById(@PathVariable("id") Integer id) {
        PatrolObjectStandard patrolObjectStandard = patrolObjectStandardService.getById(id);
        PatrolObjectStandardVo vo = new PatrolObjectStandardVo();
        BeanUtil.copyProperties(patrolObjectStandard, vo, false);
        List<AlarmRule> alarmRuleList = alarmRuleService
                .list(Wrappers.<AlarmRule>lambdaQuery().eq(AlarmRule::getStandardId, id));
        vo.setRuleList(alarmRuleList);
        return R.ok(vo);
    }


    @PostMapping
    @Operation(summary = "新增巡检对象标准值")
    public R save(@RequestBody @Validated PatrolObjectStandardDto dto) {
        List<PatrolObjectStandard> list = patrolObjectStandardService.list(Wrappers.<PatrolObjectStandard>lambdaQuery().eq(PatrolObjectStandard::getPatrolObjectId, dto.getPatrolObjectId()));
        if (!list.isEmpty()) {
            R.failed("不能重复添加标准值");
        }
        PatrolObjectStandard patrolObjectStandard = new PatrolObjectStandard();
        BeanUtil.copyProperties(dto, patrolObjectStandard, false);
        boolean save1 = patrolObjectStandardService.save(patrolObjectStandard);
        if (!save1) {
            return R.failed("新增巡检对象标准值失败!");
        }
        List<AlarmRule> alarmRules = dto.getRuleList();
        if (CollUtil.isNotEmpty(alarmRules)) {
            for (AlarmRule alarmRule : alarmRules) {
                alarmRule.setStandardId(patrolObjectStandard.getId());
            }
            boolean save2 = alarmRuleService.saveOrUpdateBatch(alarmRules);
            if (!save2) {
                return R.failed("新增告警规则失败!");
            }
        }
        return R.ok();
    }

    @PutMapping
    @Operation(summary = "修改巡检对象标准值")
    public R updateById(@RequestBody @Validated PatrolObjectStandardDto dto) {
        PatrolObjectStandard patrolObjectStandard = new PatrolObjectStandard();
        BeanUtil.copyProperties(dto, patrolObjectStandard, false);
        boolean update1 = patrolObjectStandardService.updateById(patrolObjectStandard);
        if (!update1) {
            return R.failed("修改巡检对象标准值失败!");
        }
        boolean remove = alarmRuleService.remove(Wrappers.<AlarmRule>lambdaQuery().eq(AlarmRule::getStandardId, patrolObjectStandard.getId()));
//        if (!remove) {
//            return R.failed("删除告警规则失败!");
//        }
        List<AlarmRule> alarmRules = dto.getRuleList();
        if (CollUtil.isNotEmpty(alarmRules)) {
            for (AlarmRule alarmRule : alarmRules) {
                alarmRule.setStandardId(patrolObjectStandard.getId());
            }
            boolean save2 = alarmRuleService.saveOrUpdateBatch(alarmRules);
            if (!save2) {
                return R.failed("新增告警规则失败!");
            }
        }
        return R.ok();
    }

    @DeleteMapping
    @Operation(summary = "根据id删除巡检对象标准值")
    public R removeById(@RequestBody Integer[] ids) {
        boolean remove = alarmRuleService.remove(Wrappers.<AlarmRule>lambdaQuery().in(AlarmRule::getStandardId, ids));
//        if (!remove) {
//            return R.failed("删除告警规则失败!");
//        }
        return R.ok(patrolObjectStandardService.removeBatchByIds(CollUtil.toList(ids)));
    }

}
